<template>
  <el-form
    ref="loginFormRef"
    :model="loginForm"
    status-icon
    :rules="rules"
    size="large"
  >
    <el-form-item prop="username">
      <el-input v-model="loginForm.username" placeholder="请输入用户名">
        <template #prefix>
          <el-icon class="el-input__icon">
            <user />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        type="password"
        v-model="loginForm.password"
        placeholder="请输入密码"
        show-password
      >
        <template #prefix>
          <el-icon class="el-input__icon">
            <lock />
          </el-icon>
        </template>
      </el-input>
    </el-form-item>
  </el-form>
  <div class="login-btn">
    <el-button :icon="CircleClose" round size="large" @click="resetForm"
      >重置</el-button
    >
    <el-button
      :icon="UserFilled"
      round
      size="large"
      type="primary"
      :loading="loading"
      @click="onLogin"
    >
      登录
    </el-button>
  </div>
</template>
<script setup>
import { reactive, ref, onMounted } from "vue";
import { loginApi, adminPermission } from "@/api/login/login";
import { useRouter } from "vue-router";
import { CircleClose, UserFilled } from "@element-plus/icons-vue";
import { GlobalStore } from "@/store";
import { UserStore } from "@/store/modules/system/user";
// import { ElMessage } from "element-plus";

const loginFormRef = ref();
const loading = ref(false);
const router = useRouter();

const globalStore = GlobalStore();
const userStore = UserStore();

// 校验
const rules = {
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
};
const loginData = {
  username: "",
  password: "",
  google_code: "",
};
const loginForm = reactive({ ...loginData });

// 重置
const resetForm = () => {
  loginFormRef.value.resetFields();
};

//登录
const onLogin = () => {
  loginFormRef.value
    .validate()
    .then(async () => {
      try {
        loading.value = true;
        // 1.调用登录接口
        const res = await loginApi(loginForm);
        // // 2.更新用户信息到pinia

        userStore.setUserInfo(res.data);
        globalStore.setToken(res.data.auth_token);
        globalStore.walletSelectConfig();
        globalStore.setMerchant();
        globalStore.setPayMerchant();
        const res2 = await adminPermission();
        console.log(res2);
        // 3.调整到首页
        router.replace("/");
        ElMessage({
          message: "欢迎登录",
          type: "success",
        });
      } catch (error) {
        loading.value = false;
        console.log(error);
      }
    })
    .catch((error) => {
      console.log(error);
    });
};

onMounted(() => {
  // 监听enter事件（调用登录）
  document.onkeydown = (e) => {
    e = window.event || e;
    if (e.code === "Enter" || e.code === "enter" || e.code === "NumpadEnter") {
      if (loading.value) return;
      onLogin();
    }
  };
});
</script>
<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 40px;
}

.login-btn {
  display: flex;
  justify-content: space-between;
  width: 100%;
  margin-top: 40px;
  white-space: nowrap;

  .el-button {
    width: 170px;
  }
}
</style>
